<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>

<?php

	/*
		Jax Calendar v1.33 - Copyright (c) 2002-2004, Andreas John aka Jack (tR)
		This program and it's moduls are Open Source in terms of General Public Licens (GPL) v2.0

		dbupdate.php 		(CSV data update system)
	*/
	
	$update_at_once = 30;
	
	
    // fit if nescessary:
	define( "PATH_TO_SCRIPT", "../" );

	// global variables (have to be fitted)
	require ( dirname(__FILE__) . "/".PATH_TO_SCRIPT."settings/calendar.settings.inc.php");

	// (If exists) include localization file for the favoured language
	if ( file_exists( dirname(__FILE__) . "/".PATH_TO_SCRIPT."languages/" . $language . ".inc.php" ) )
		define( "LOC_LANG", $language ); 
	else
		define( "LOC_LANG", $default_language ); 

	require_once ( dirname(__FILE__) ."/".PATH_TO_SCRIPT."languages/" . LOC_LANG . ".inc.php" );

	if (!empty( $_SERVER["PHP_SELF"] )) $PHP_SELF = $_SERVER["PHP_SELF"];
	
	$ox_tail = "cal_id=$cal_id&language=$language";	
	
?>

<head>
	<title><?php echo $loc_lang["calendar"]." - ".$loc_lang["admin_program"]; ?></title>
	<meta http-equiv="Content-Type" content="text/html; charset=<?php echo $charset ?>">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="expires" content="0">	

	<link rel="stylesheet" href="<?php echo PATH_TO_SCRIPT; ?>css/admin.css">
</head>

<body >
<div class="admin">
<div class="title"><?php echo $loc_lang["calendar"]." - ".$loc_lang["admin_program"]; ?></div>

<?php

	if ( $data_source != "csvfile" ) { echo "Datasource is no CSV file!"; exit; }
	else
	{

		// include class for CSV-textfile support
		require ( PATH_TO_SCRIPT."modules/classes/csvfile.inc.php" );

		// prepeare objects for file access
		// list of subscribers which signed in/off
		$calendar_list        = new csvfile;
		$calendar_list->name  = PATH_TO_SCRIPT.'cal_data/'.$calendar_file;
		$calendar_list->init();
		
		$event_list        = new csvfile;
		$event_list->name  = PATH_TO_SCRIPT.'cal_data/'.$event_file;
		$event_list->init();
	}

	
	if (!empty( $_GET["do"] ) ) $do = $_GET["do"]; else $do = "start";
	
	if ( $do == "start" )
	{
		echo "<p>This programm will update your .CSV file for cal_id=$cal_id and save results to a dummy copy file!</p><p>&nbsp;</p>";
		if (!$fh = fopen( "dummy_copy.csv","w" ) ) 
			die ("<p>&nbsp;</p><font color=\"red\"><b>Cannot open file 'admin/dummy_copy.csv' ... Write access is required for this file! Please CHMOD 777!</b></font><p>&nbsp;</p>");
		fclose( $fh );
		
		$do = "continue";	
		
	}
	
	
	if ( $do == "continue" )
	{
		$new_calendar_list        = new csvfile;
		$new_calendar_list->name  = "dummy_copy.csv";
		$new_calendar_list->init();				
		
		
		$src_entries = $calendar_list->entries(); if ($src_entries == 0) exit;

		$src_red_data = array();
		
		if (!empty($_GET["start_at"])) $i = $_GET["start_at"]; else $i = 0;
		echo "<p>Conversion in progress... $i of $src_entries</p>";
		
		$processed = 0;
		$calendar_list->get_entry( $i, $src_red_data );
		
		do
		{

			$new_event_start = $src_red_data["event_start"];
//----------------------------------------------------------------			

			$dst_red_data = array();
			$dst_entries = $new_calendar_list->entries();			

			$new_calendar_list->get_entry($j,$dst_red_data);
			$pos = 0;

			while ( $pos<=$dst_entries && ($new_event_start < $dst_red_data["event_start"]  ) )
			{
				$new_calendar_list->get_next_entry( $dst_red_data );
				$pos++;
			} 
				
			$new_calendar_list->insert( $pos, $src_red_data );
			$processed++;
				
			if ($processed >= $update_at_once) 
			{
				echo '<meta http-equiv="refresh" content="0;URL='.$PHP_SELF."?$ox_tail&do=continue&start_at=".($i+1).'">';
				exit;	
			}
	
//---------------------------------------------------------------------			
		
			$calendar_list->get_next_entry( $src_red_data );
			$i++;
			
		} while ($i<$src_entries);
		
		echo "<p><b>Process completed!</b><br></p><p>&nbsp;</p>";
		echo '<meta http-equiv="refresh" content="0;URL='.$PHP_SELF."?$ox_tail&do=recopy".'">';		
	}
	
	
	if ( $do == "recopy" )
	{
		$csvcontent = file( "dummy_copy.csv" );
		$cal_file = fopen( PATH_TO_SCRIPT.'cal_data/'.$calendar_file, "w" );
		fwrite( $cal_file, ereg_replace( "\r", "", implode( "", $csvcontent ) ) );
		fclose( $cal_file );

		echo "<p><b>Update completed!</b><br></p><p>&nbsp;</p>";
		echo '<meta http-equiv="refresh" content="10;URL=index.php?$ox_tail"'.'">';
		echo '<p><a href="index.php?$ox_tail">Return to Admin System</a></p>';
	}
	
	

?>
</div>
<!-- Please don't remove the link to the author's website!!! -->
<p class="author_info">Jax Calendar v1.33 by Jack (tR), <a href="http://www.jtr.de/scripting/php">www.jtr.de/scripting/php</a></p>
</body>

</html>